package com.fly.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fly.common.R;
import com.fly.pojo.User;
import com.fly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R<User> login(@RequestBody Map ue, HttpServletRequest request){
        User user = new User();
        user.setUsername(String.valueOf( ue.get("user")));
        user.setPassword(String.valueOf( ue.get("password")));
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.eq(User::getUsername,user.getUsername());//将 user.getUserName()传给qqueryWrapper
        User emp =userService.getOne(queryWrapper);

        if (emp==null){
            return R.error("登录失败");
        }
        if (!emp.getPassword().equals(user.getPassword())){
            return R.error("登录失败");
        }
            request.getSession().setAttribute("id",emp.getId());

        return  R.success(emp);
    }

}
